﻿using System;

namespace TetrisGame
{
    /// <summary>
    /// 场景类型枚举 开始、游戏、结束
    /// </summary>
    internal enum E_SceneType
    {
        Start,
        Difficulty,
        Game,
        End
    }

    internal class Game
    {
        public const int Width = 50; // Width of the game area
        public const int Height = 30; // Height of the game area
        public static IUpdateScene nowScene; // 当前场景
        private static E_DifficultyLevel difficultyLevel; // 游戏难度

        // 构造函数
        public Game()
        {
            // 初始化控制台窗口
            Console.SetWindowSize(Width, Height);
            Console.SetBufferSize(Width, Height);
            // 隐藏光标
            Console.CursorVisible = false;
            // 设置标题
            Console.Title = "Tetris Game";
            // 设置背景颜色
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();
            // 设置游戏为开始场景
            ChangeScene(E_SceneType.Start);
        }

        // 游戏入口
        public void Start()
        {
            while (true)
            {
                // 更新当前场景
                nowScene?.Update();
            }
        }

        public static void SelectDifficulty(int difficulty)
        {
            // 根据难度设置游戏速度
            switch (difficulty)
            {
                case 0:
                    difficultyLevel = E_DifficultyLevel.Easy;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 1:
                    difficultyLevel = E_DifficultyLevel.Medium;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 2:
                    difficultyLevel = E_DifficultyLevel.Hard;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 3:
                    difficultyLevel = E_DifficultyLevel.Crazy;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 4:

                    ChangeScene(E_SceneType.Start);
                    break;
            }
        }

        // 切换场景
        public static void ChangeScene(E_SceneType sceneType)
        {
            // 切换场景之前先清除当前场景
            Console.Clear();

            switch (sceneType)
            {
                case E_SceneType.Start:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    nowScene = new StartScene();// 初始化开始场景
                    break;

                case E_SceneType.Game:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    nowScene = new GameScene(difficultyLevel);// 初始化开始场景
                    break;

                case E_SceneType.End:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    nowScene = new EndScene();// 初始化开始场景
                    break;

                case E_SceneType.Difficulty:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    nowScene = new DifficultySelect(); // 初始化难度选择场景
                    break;
            }
        }
    }
}